package com.ruoyi.web.controller.datastatistics;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.basedata.domain.TerminalsXb;
import com.ruoyi.basedata.mapper.TerminalsXbMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.datastatistics.domain.MeterHistoryData;
import com.ruoyi.datastatistics.domain.TerminalHistoryData;
import com.ruoyi.datastatistics.mapper.MeterHistoryDataMapper;
import com.ruoyi.datastatistics.mapper.TerminalHistoryDataMapper;
import com.ruoyi.datastatistics.service.IMeterHistoryDataService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

/**
 * 小-历史数据Controller
 *
 * @author ruoyi
 * @date 2023-09-06
 */
@RestController
@RequestMapping("/datastatistics/meterhistory")
public class MeterHistoryDataController extends BaseController {
    @Autowired
    private IMeterHistoryDataService meterHistoryDataService;
    @Autowired
    private MeterHistoryDataMapper meterHistoryDataMapper;
    @Autowired
    private TerminalHistoryDataMapper terminalHistoryDataMapper;
    @Autowired
    private TerminalsXbMapper terminalsXbService;
    /**
     * 查询小-历史数据列表
     */
//    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(MeterHistoryData meterHistoryData) throws ParseException {
//        startPage();
//        List<MeterHistoryData> list = meterHistoryDataService.selectMeterHistoryDataList(meterHistoryData);
//        return getDataTable(list);
//    }


    /**
     * 查询小-历史数据列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:list')")
    @GetMapping("/list")
    public TableDataInfo getList(MeterHistoryData meterHistoryData) throws ParseException {
        startPage();
        List<MeterHistoryData> list = meterHistoryDataService.selectList(meterHistoryData);
        return getDataTable(list);
    }

    /**
     * 导出小-历史数据列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:export')")
    @Log(title = "小-历史数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeterHistoryData meterHistoryData) throws ParseException {


        TerminalsXb terminalsXb = new TerminalsXb();
        terminalsXb.setLocationNb(meterHistoryData.getLocationNb());
        terminalsXb.setTerminalMark(1);
        List<TerminalsXb> terminalsXbs = terminalsXbService.selectTerminalsXbList(terminalsXb);
        if (CollectionUtils.isNotEmpty(terminalsXbs)){
            terminalsXb = terminalsXbs.get(0);
        }
        List<MeterHistoryData> list = meterHistoryDataService.selectList(meterHistoryData);
        TerminalsXb finalTerminalsXb = terminalsXb;
        list.forEach(data ->{
            data.setUserName(finalTerminalsXb.getUserName());
            data.setUserId(finalTerminalsXb.getUserId());
            data.setUserAddress(finalTerminalsXb.getUserAddress());
        });
        ExcelUtil<MeterHistoryData> util = new ExcelUtil<MeterHistoryData>(MeterHistoryData.class);
        util.exportExcel(response, list, "小-历史数据数据");
    }

    /**
     * 获取小-历史数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(meterHistoryDataService.selectMeterHistoryDataByID(ID));
    }

    /**
     * 新增小-历史数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:add')")
    @Log(title = "小-历史数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeterHistoryData meterHistoryData) {
        return toAjax(meterHistoryDataService.insertMeterHistoryData(meterHistoryData));
    }

    /**
     * 修改小-历史数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:edit')")
    @Log(title = "小-历史数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeterHistoryData meterHistoryData) {
        return toAjax(meterHistoryDataService.updateMeterHistoryData(meterHistoryData));
    }

    /**
     * 删除小-历史数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:remove')")
    @Log(title = "小-历史数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(meterHistoryDataService.deleteMeterHistoryDataByIDs(IDs));
    }

    @GetMapping("/test1")
    public AjaxResult test1() {
        return success(meterHistoryDataMapper.selectList(new LambdaQueryWrapper<MeterHistoryData>()
                .eq(MeterHistoryData::getId, 34L)));
    }

    @GetMapping("/test2")
    public AjaxResult test2() {
        return success(terminalHistoryDataMapper.selectList(new LambdaQueryWrapper<TerminalHistoryData>()
                .eq(TerminalHistoryData::getId, 34L)));
    }
}
